﻿using System;
using CPN.PJN.Silverlight.Common.Configuration;
using CPN.PJN.Silverlight.Common.ExceptionHandling;
using CPN.PJN.Silverlight.Common.ExceptionHandling.Configuration;
using CPN.PJN.Silverlight.Common.Logging.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CPN.PJN.Common.ExceptionHandling.UnitTests
{
    [TestClass]
    public class Tests
    {
        public Uri ConfigurationUri
        {
            get
            {
                return new Uri(@"http://localhost:8888/clientbin/Silverlight.Core.xml", UriKind.RelativeOrAbsolute);
            }
        }

        [TestMethod]
        public void TestMethod1_ExceptionConfigurationManager()
        {
            ConfigurationManager.Instance.ConfigurationFilename = this.ConfigurationUri;
            ConfigurationManager.Instance.ConfigurationDownloadCompleted += new EventHandler<ConfigurationDownloadCompletedArgs>(ExceptionConfigurationManagerConfigurationDownloadCompleted);
            ConfigurationManager.Instance.DownloadConfiguration();
        }
        void ExceptionConfigurationManagerConfigurationDownloadCompleted(object sender, ConfigurationDownloadCompletedArgs e)
        {
            // Configuration du Gestionnaire d'Exception
            ExceptionConfigurationManager.Instance.Configure();

            // Véirife que la configuration comporte plus d'une politique d'exception
            Assert.IsTrue(ExceptionConfigurationManager.Instance.PolicyDictionary.Count > 0);

            ConfigurationManager.Instance.ConfigurationDownloadCompleted -= ExceptionConfigurationManagerConfigurationDownloadCompleted;
        }


        [TestMethod]
        public void TestMethod2_ExceptionManager()
        {
            ConfigurationManager.Instance.ConfigurationFilename = this.ConfigurationUri;
            ConfigurationManager.Instance.ConfigurationDownloadCompleted += new EventHandler<ConfigurationDownloadCompletedArgs>(ExceptionManagerConfigurationDownloadCompleted);
            ConfigurationManager.Instance.DownloadConfiguration();
        }
        void ExceptionManagerConfigurationDownloadCompleted(object sender, ConfigurationDownloadCompletedArgs e)
        {
            // Configuration du Gestionnaire d'Exception
            ExceptionConfigurationManager.Instance.Configure();
            try
            {
                throw new NotImplementedException("this is a test ! ");
            }
            catch (Exception innerException)
            {
                var rethrown = ExceptionManager.Instance.HandleException(innerException, ExceptionManager.DEFAULT_POLICY, "valeur1");
            }
            ConfigurationManager.Instance.ConfigurationDownloadCompleted -= ExceptionManagerConfigurationDownloadCompleted;
        }

        [TestMethod]
        public void TestMethod2_ExceptionManagerWithLogger()
        {
            ConfigurationManager.Instance.ConfigurationFilename = this.ConfigurationUri;
            ConfigurationManager.Instance.ConfigurationDownloadCompleted += new EventHandler<ConfigurationDownloadCompletedArgs>(ExceptionManagerLoggerConfigurationDownloadCompleted);
            ConfigurationManager.Instance.DownloadConfiguration();
        }
        void ExceptionManagerLoggerConfigurationDownloadCompleted(object sender, ConfigurationDownloadCompletedArgs e)
        {
            // Configuration du gestionnaire de log
            LoggerConfigurationManager.Instance.Configure();

            // Configuration du Gestionnaire d'Exception
            ExceptionConfigurationManager.Instance.Configure();

            try
            {
                throw new NotImplementedException("this is a log Error ! ");
            }
            catch (Exception innerException)
            {
                var rethrown = ExceptionManager.Instance.HandleException(innerException, ExceptionManager.DEFAULT_POLICY, "valeur1");
            }
            ConfigurationManager.Instance.ConfigurationDownloadCompleted -= ExceptionManagerLoggerConfigurationDownloadCompleted;
        }
    }
}